package cn.edu.scau.cmi.tangqingning.safeComponent;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class SafeController {

    @FXML
    private TextArea textArea;
    @FXML
    private TextField delText;
	Composite root = new Composite("root"); //生成树根root ，根上长出两叶LeftA 和 LeftB


    @FXML
    void onAddLeafClick(ActionEvent event) {
    	int num = (int)(1+Math.random()*(1000));
        root.Add(new Leaf("Leaf" +String.valueOf(num)));
        textArea.setText("Successfully add a Leaf" + String.valueOf(num));
    }

    @FXML
    void onAddCompositeClick(ActionEvent event) {
    	int num = (int)(1+Math.random()*(1000));
        Composite comp = new Composite("Composite" +String.valueOf(num));    //根上长出分枝Composite X,分枝上也有两叶LeafXA 和LeafXB

    	int num1 = (int)(1+Math.random()*(1000));
        comp.Add(new Leaf("Leaf" + String.valueOf(num1)));
        root.Add(comp);
        textArea.setText("Successfully add a Composite" +String.valueOf(num) + " and a Leaf" + String.valueOf(num1));
    }

    @FXML
    void onDeleteClick(ActionEvent event) {
    	String name = delText.getText();
    	Composite c = new Composite(name);
    	if(root.Remove(c)) {
    		textArea.setText("Successfully delete the " + name);
    	} else {
    		textArea.setText("Failed delete the " + name + " because it is not existing!");
    	}
    }

    @FXML
    void onDisplayClick(ActionEvent event) {
        textArea.setText(root.Display(1).toString());    //显示大树的样子
    }

}

